#!/bin/bash
set -e
set -u
#
# This systemtest tests the oVirt plugin functionality
# of the Susan FD by using the supplied module
#   SusanFdPluginOvirt.py
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-susan-fd
#shellcheck source=../environment.in
. ./environment

JobName=backup-susan-fd
#shellcheck source=../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup


curl -k -o ${SUSAN_CONFIG_DIR}/ovirt-ca.cert "https://$OVIRT_SERVER/ovirt-engine/services/pki-resource?resource=ca-certificate&format=X509-PEM-CA"

# Directory to backup.
# This directory will be created by setup_data().
# BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
# setup_data

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
run job=$JobName level=Full yes
status director
status client
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
wait
restore client=susan-fd fileset=OvirtTest where=$tmp/susan-restores pluginoptions=@python_module_name@:local=yes select all done
yes
wait
messages
quit
END_OF_DATA

run_susan "$@"
check_for_zombie_jobs storage=File
stop_susan

check_two_logs

# Check restored disk image content
restored_disk_image=$(find $tmp/susan-restores -type f | grep -v "\.ovf" | tail -1)
if [ "$(strings "$restored_disk_image" | tail -1)" = "SusanTest" ]; then
    echo "Restored disk image content OK"
else
    echo "Restored disk image ERROR: Does not contain expected string"
    dstat=1
fi

end_test
